package com.jfinal.code.eclipse.plugin.wizards.ui;

import java.util.Arrays;

import org.apache.commons.lang.ArrayUtils;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;

import com.jfinal.code.eclipse.plugin.i18n.I18n;
import com.jfinal.code.eclipse.plugin.wizards.page.TableSelectWizardPage;

/**
 * 让NPE见鬼去吧。<p>
 * 创建数据库表选择引导页的一系列UI
 * @author yaolin
 *
 */
public class TableSelectUI {

	private final TableSelectWizardPage page;
	private final Shell shell;

	public TableSelectUI(TableSelectWizardPage page, Shell shell) {
		this.page = page;
		this.shell = shell;
	}

	/**
	 * 创建初始表List
	 */
	public List createOrdinalList(Composite parent) {
		List list = new List(parent, SWT.MULTI | SWT.V_SCROLL);
		list.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
		return list;
	}

	/**
	 * 创建全部选中按钮
	 */
	public Button createAllToRightButton(Composite parent) {
		Button btn = new Button(parent, SWT.PUSH);
		btn.setText(" > > ");
		btn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		btn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				page.getSelectedList().setItems(page.getListItems());
				page.getOrdinalList().removeAll();
				Validator.validateEmptyItem(page);
			}
		});
		return btn;
	}

	/**
	 * 创建选中按钮
	 */
	public Button createToRightButton(Composite parent) {
		Button btn = new Button(parent, SWT.PUSH);
		btn.setText("  >  ");
		btn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		btn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String[] selectItems = page.getOrdinalList().getSelection();
				String[] items = page.getSelectedList().getItems();
				String[] listItems = (String[]) ArrayUtils.addAll(items, selectItems);
				Arrays.sort(listItems);
				page.getSelectedList().setItems(listItems);
				for (String item : selectItems) {
					page.getOrdinalList().remove(item);
				}
				Validator.validateEmptyItem(page);
			}
		});
		return btn;
	}

	/**
	 * 创建取消按钮
	 */
	public Button createToLeftButton(Composite parent) {
		Button btn = new Button(parent, SWT.PUSH);
		btn.setText("  <  ");
		btn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		btn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String[] selectItems = page.getSelectedList().getSelection();
				String[] items = page.getOrdinalList().getItems();
				String[] listItems = (String[]) ArrayUtils.addAll(items, selectItems);
				Arrays.sort(listItems);
				page.getOrdinalList().setItems(listItems);
				for (String item : selectItems) {
					page.getSelectedList().remove(item);
				}
				Validator.validateEmptyItem(page);
			}
		});
		return btn;
	}

	/**
	 * 创建全部取消按钮
	 */
	public Button createAllToLeftButton(Composite parent) {
		Button btn = new Button(parent, SWT.PUSH);
		btn.setText(" < < ");
		btn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		btn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				page.getOrdinalList().setItems(page.getListItems());
				page.getSelectedList().removeAll();
				Validator.validateEmptyItem(page);
			}
		});
		return btn;
	}

	/**
	 * 创建已经选择的表List
	 */
	public List createSelectedList(Composite parent) {
		List list = new List(parent, SWT.MULTI | SWT.V_SCROLL);
		list.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
		return list;
	}

	/**
	 * 选择适配
	 * @author yaolin
	 */
	class BrowseSelectionAdapter extends SelectionAdapter {
		private final String title;
		private final Text text;
		public BrowseSelectionAdapter(String title, Text text) {
			this.title = title;
			this.text = text;
		}
		@Override
		public void widgetSelected(SelectionEvent e) {
			ContainerSelectionDialog dialog = new ContainerSelectionDialog(shell, ResourcesPlugin.getWorkspace().getRoot(), false, title);
			if (dialog.open() == ContainerSelectionDialog.OK) {
				Object[] result = dialog.getResult();
				if (result.length == 1) {
					text.setText(((Path) result[0]).toString());
				}
			}
		}
	}
	
	/**
	 * 非空校验
	 * @author yaolin
	 */
	public static class Validator {
		public static void validateEmptyItem(TableSelectWizardPage page) {
			if (page.getSelectedList().getItemCount() == 0) {
				updateStatus(page, I18n.tableSelectUI_pleaseSelectTable);
			} else {
				updateStatus(page, null);
			}
		}
		private static void updateStatus(TableSelectWizardPage page, String message) {
			page.setErrorMessage(message);
			page.setPageComplete(message == null);
		}
	}
}
